import 'dart:math';

printIntager(int num) {
  print("this number is $num");
}

//var用于指定变量，不指定类型 dynamic不确定的类型
//_number 标识符用下划线开头的标拾库内私有 类似于private
main() {
  var number = 15;
  printIntager(number);

  var lsit = [1, 2, 3];
  var listData = new List();
  List data = List();
  data.addAll(listData);

  var list1 = [0, ...lsit]; //0，1，2，3

  List emptyList = null;
  var list2 = [0, ...?emptyList];

  //collection if
  var listif = [2, 6, if (2 > 3) 4];

  //collection for
  var forData = [1, 2, 3];
  var collFor = ["0", "25", for (var item in forData) "$item"];

  print(collFor);

  //set 无须且唯一的元素集合，支持set字面量以及set类型两种形式

  var set1 = {"1"};
  var set2 = <int>{}; //创建空的set要加泛型<int>{}  map的空形式是{}
  Set<int> set3 = new Set();

  //map
  var map1 = {0: "a", 1: "2"};
  Map<String, int> map2 = new Map();
  map2["a"] = 5;

  // add('a', 'v');
  // testPrint();
  // operationTset();
  // assertTest();
  // forTest();
  // try {
  //    testPersion();
  // } catch (e) {
  //   print(e);
  // }
  testClass();
}

//方法 可选参数 {}  调用的时候 add('a','v',type:'is');
//在flutter里面使用@required注解来标识一个命名参数是必须的参数
//命名参数可以提供默认值 void add(String name,String age ,{String type = '444'})
void add(String name, String age, {String type}) {
  print("$name $age ${type ?? 'type is empty'}");
}

//位置参数  用[]  void add(String name,String age ,[String type = '',String sex])
//也可以给默认值 没有指定的情况未null
// add('a','v','s');

//函数做位一级对象
void printE(int a) {
  print(a);
}

void testPrint() {
  var list = {1, 2, 3};
  list.forEach(printE);
  //函数变量
  var a = (e) {
    return "aaaa";
  };
  var b = (e) => "dawda $e"; //胖箭头语法
  print(b("dawdadawd"));
  var c = (String e) => "wwwww$e";

  var add = basicAdd(15);//返回是一个function
  print(add(10));
}

//词法闭包
// var add = basicAdd(15);//返回是一个function
// print(add(10)); 输出 25
basicAdd(int basicValue){
  return (int e) =>basicValue+e;
}



void operationTset(){
  //运算符 
  //注意 / 和 ~/的区别
  var a  = 5;
  var b = 2;
  var c = a/b;
  print(c);//输出 2.5
  var d = a~/b;
  print(d);//输出 2

  //级联运算符 .. 表示返回的自己
  A aa = A();
  aa.a = 12;
  aa.b = 1.5;
  aa.c ="aaaa";

  A bb = A();
  bb..a = 5
  ..b = 5.5
  ..c = "sawaaa";
}

class A{
  int a = 0;
  double b = 0.0;
  String c = "aaa";
}


//流程控制语句
String playerName(String name){
  if(null!=name){
    return name;
  }else{
    return 'Guster';
  }
}

String playerName1(String name){
 return name==null?"Guster":name;
}

String playerName3(String name){
  return name?? "Guster";
}


void forTest(){
  var listData = [10,20,30];
  for (var i = 0; i < listData.length; i++) {
    print(listData[i]);
  }

  listData.forEach((e){
    print(e);
  });
  
  for (var item in listData) {
     print(item);
  }
}

void assertTest(){
  String currUrl = "http://www.baidu.com";
  //中断语句（测试用的）
  assert(currUrl.startsWith("https"));
}

class Persion{
  String name;
  String sex;
  int age;
  Persion(this.name,this.sex,this.age);

  @override
  String toString() {
    return 'name: $name  sex:$sex age:$age';
  }
}


void testPersion(){
  var mPersion = Persion('xiaohao', 'sex', 5);
  print(mPersion);
  throw Exception("hahahha");
}

//工厂构造函数 factory
class Logger{
  final String name;
  static final Map<String,Logger> _cache = <String,Logger>{};
  factory Logger(String name){
    return _cache.putIfAbsent(name, ()=>Logger._internal(name));
  }
  Logger._internal (this.name);
}

//继承 调用父类的构造函数 初始化列表 =>  父类的构造函数 =>子类的构造函数
//输出：Student => GoodStudent

class Student{
  String name;
  Student.fromJson(Map data){
    print('Student');
  }
}

class GoodStudent extends Student{
  GoodStudent.fromJson(Map data) : super.fromJson(data){
    print("GoodStudent");
  }
}



//实例方法  可以访问实例变量和this
class Point{
  int x;
  int y;
  Point(this.x,this.y);

  num distanceTo(Point point){
    var dx = x - point.x;
    var dy = y - point.y;
    return sqrt(dx*dx+dy*dy);
  }
}

//get 和 set 方法
class Rectangle{
  num left,top,right,width;
  Rectangle(this.left,this.top,this.right,this.width);
  //num get right=> left+width;
  //set right(num value) =>left = value-width;
  
}


//mixin 关键字的使用
abstract class Animal{}

abstract class Bird extends Animal {}

mixin Walker{
  void walker(){
    print('I  walker');
  }
}

mixin Flyer{
  void fly(){
    print('I fly');
  }
}

class Parrot extends Bird with Walker,Flyer{}

//输出I fly  I  walker
//  Parrot parrot = new Parrot();
//     parrot.fly();
//     parrot.walker();


void testClass(){
  var mGoodStudent  = GoodStudent.fromJson({});

  var p1 = Point(1, 1);
  var p2 = Point(5, 9);

  print(p1.distanceTo(p2));

    Parrot parrot = new Parrot();
    parrot.fly();
    parrot.walker();
}






